//
//  AECommunityPublishViewModel.m
//  AESOLAR_IOS
//
//  Created by Bin Tang on 2024/10/18.
//  Copyright © 2024 Mr.Wang. All rights reserved.
//

#import "AECommunityPublishViewModel.h"

@implementation AECommunityPublishViewModel

- (void)initialize {
    [super initialize];
    
    self.addImageVideoNum = 9;
    
    _reqModel = [AENewsAddReqModel new];
}


- (float)imageWH
{
    if (_imageWH)
    {
        return _imageWH;
    }
    
    _imageWH = (kScreenWidth - 15 * 2 - 4 * 2) / 3.0;
    return _imageWH;
}

/**
 得到图片的大小
 
 @param model model description
 @return return value description
 */
- (CGSize)getImageSize:(EDSImageVideoModel *)model
{
    CGSize imgSize = CGSizeZero;
    if (self.libType == 0)
    {// 图片
//        int itemWH = (kScreenWidth - 30 - 2 * 15) / 3;
        imgSize = CGSizeMake(self.imageWH, self.imageWH);
    }
    else if (self.libType == 1)
    {// 视频
        CGSize size = model.srcImage.size;
        if (size.width > size.height)
        {
            imgSize = CGSizeMake(156, 156.0 / size.width * size.height);
        }
        else
        {
            imgSize = CGSizeMake(96, 96.0 / (1.0*size.width/size.height));
        }
    }
    return imgSize;
}

/**
 图片的视图的总高度

 @return return value description
 */
- (CGFloat)imagesHeight
{
    // 图片
    if (self.dataList.count <= 3)
    {
        return self.imageWH;
    }
    else if (self.dataList.count > 3 && self.dataList.count <= 6)
    {
        return self.imageWH * 2 + 4;
    }
    else
    {
        return self.imageWH * 3 + 4 * 2;
    }
    return self.imageWH;
}

/// 上传图片
/// - Parameters:
///   - imageModel: imageModel description
///   - completion: completion description
- (void)requestWithUploadImage:(EDSImageVideoModel *)imageModel completion:(RequestCompletionBlock)completion {
    @weakify(self);
    AEUploadFileRequest *request = [[AEUploadFileRequest alloc] initWithFiles:@[imageModel]];
    [request startWithCompletionBlockWithSuccess:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        EDSRespondDataEntity *respond = request.responseModelObject;
        if (respond.code == EDS_NETWORK_SUCCESS) {
            AENewsInfoOssModel *model = respond.data;
            [self.reqModel.ossIds addObject:@(model.id)];
        }
        [self sendResponseObject:request.responseModelObject completion:completion];
    } failure:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        [self sendResponseObject:request.responseModelObject completion:completion];
    }];
}

/// 添加新闻
/// @param completion completion description
- (void)requestWithNewsAdd:(RequestCompletionBlock)completion {
    @weakify(self);
    AENewsAddRequest *request = [[AENewsAddRequest alloc] initWithModel:self.reqModel];
    [request startWithCompletionBlockWithSuccess:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        EDSRespondDataEntity *respond = request.responseModelObject;
        if (respond.code == EDS_NETWORK_SUCCESS) {
            
        }
        [self sendResponseObject:request.responseModelObject completion:completion];
    } failure:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        [self sendResponseObject:request.responseModelObject completion:completion];
    }];
}

@end
